package edu.bbu.license.client.android.gui.files;

import java.util.LinkedList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;
import android.widget.ProgressBar;
import android.widget.TextView;
import edu.bbu.license.client.android.R;
import edu.bbu.license.client.android.controller.files.FilesController;
import edu.bbu.license.client.android.gui.BaseActivity;
import edu.bbu.license.client.android.gui.files.adapter.FilesActivityFilesGridAdapter;
import edu.bbu.license.client.android.gui.files.adapter.FilesActivityPageAdapter;
import edu.bbu.license.client.android.gui.files.dialogs.LocalFilePressDialogActivity;
import edu.bbu.license.client.android.gui.files.dialogs.NewFolderDialogActivity;
import edu.bbu.license.client.android.gui.files.dialogs.ServerFilePressDialogActivity;
import edu.bbu.license.client.android.gui.guest.GuestActivity;
import edu.bbu.license.client.android.gui.settings.SettingsActivity;
import edu.bbu.license.client.android.gui.transfer.FileTransferActivity;
import edu.bbu.license.client.android.services.file.model.FileModel;

public class FilesActivity extends BaseActivity implements FilesView {

	private ViewPager viewPager;
	private LinkedList<View> viewPagerList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.files_activity);
		
		viewPagerList = new LinkedList<View>();
		viewPagerList.add(getLayoutInflater().inflate(R.layout.files_activity_local_files_page, null));
		viewPagerList.add(getLayoutInflater().inflate(R.layout.files_activity_server_files_page, null));
		
		viewPager = (ViewPager) findViewById(R.id.files_activity_pager);
		viewPager.setAdapter(new FilesActivityPageAdapter(viewPagerList));		
		
		FilesController.getInstance().atach(this);
	}
	
	@Override
	public void openGuestView() {
		startActivity(new Intent(this, GuestActivity.class));
	}
	
	@Override
	public void closeFilesView() {
		finish();		
	}
	
	@Override
	public void openFileTrafficView() {
		startActivity(new Intent(this, FileTransferActivity.class));
	}
	
	@Override
	public void openSettingsView() {
		startActivity(new Intent(this, SettingsActivity.class));
	}
	
	@Override
	public void openNewFolderDialog() {
		startActivity(new Intent(this, NewFolderDialogActivity.class));
	}
	
	@Override
	public void openLocalFilesPressDialog() {
		startActivity(new Intent(this, LocalFilePressDialogActivity.class));
	}
	
	@Override
	public void openServerFilesPressDialog() {
		startActivity(new Intent(this, ServerFilePressDialogActivity.class));
	}
	
	@Override
	public void setHeaderLogoutButtonListener(OnClickListener onClickListener) {
		findViewById(R.id.files_activity_header_logout_button).setOnClickListener(onClickListener);
	}
	
	@Override
	public void setHeaderTrafficButtonListener(OnClickListener onClickListener) {
		findViewById(R.id.files_activity_header_traffic_button).setOnClickListener(onClickListener);
	}
	
	@Override
	public void setHeaderSettingsButtonListener(OnClickListener onClickListener) {
		findViewById(R.id.files_activity_header_settings_button).setOnClickListener(onClickListener);
		
	}
	
	@Override
	public void setBackButtonListener(OnClickListener onClickListener) {
		findViewById(R.id.files_activity_footer_back_button).setOnClickListener(onClickListener);		
	}
	
	@Override
	public void setRefreshButtonListener(OnClickListener onClickListener) {
		findViewById(R.id.files_activity_footer_refresh_button).setOnClickListener(onClickListener);		
	}
	
	@Override
	public void setNewDirectoryButtonListener(OnClickListener onClickListener) {
		findViewById(R.id.files_activity_footer_newdirectory_button).setOnClickListener(onClickListener);		
	}
	
	@Override
	public void setSynchronizeButtonListener(OnClickListener onClickListener) {
		findViewById(R.id.files_activity_footer_syncronize_button).setOnClickListener(onClickListener);		
	}
	
	@Override
	public void setLocalFilesItemClickListener(OnItemClickListener onItemClickListener) {
		GridView gridView = (GridView) viewPagerList.get(LOCAL_FILES_PAGE).findViewById(R.id.files_activity_local_files_grid);
		gridView.setOnItemClickListener(onItemClickListener);
	}
	
	@Override
	public void setServerFilesItemClickListener(OnItemClickListener onItemClickListener) {
		GridView gridView = (GridView) viewPagerList.get(SERVER_FILES_PAGE).findViewById(R.id.files_activity_server_files_grid);
		gridView.setOnItemClickListener(onItemClickListener);		
	}
	
	@Override
	public void setLocalFilesItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
		GridView gridView = (GridView) viewPagerList.get(LOCAL_FILES_PAGE).findViewById(R.id.files_activity_local_files_grid);
		gridView.setOnItemLongClickListener(onItemLongClickListener);
	}
	
	@Override
	public void setServerFilesItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
		GridView gridView = (GridView) viewPagerList.get(SERVER_FILES_PAGE).findViewById(R.id.files_activity_server_files_grid);
		gridView.setOnItemLongClickListener(onItemLongClickListener);
	}
	
	@Override
	public boolean isLocalFilesViewActive() {
		return viewPager.getCurrentItem() == LOCAL_FILES_PAGE;
	}
	
	@Override
	public boolean isServerFilesViewActive() {
		return viewPager.getCurrentItem() == SERVER_FILES_PAGE;
	}
	
	@Override
	public void fillLocalFilesGrid(final List<FileModel> fileList) {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				GridView gridView = (GridView) viewPagerList.get(LOCAL_FILES_PAGE).findViewById(R.id.files_activity_local_files_grid);
			    gridView.setAdapter(new FilesActivityFilesGridAdapter(fileList));				
			}
		});
	}
	
	@Override
	public void fillServerFilesGrid(final List<FileModel> fileList) {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				GridView gridView = (GridView) viewPagerList.get(SERVER_FILES_PAGE).findViewById(R.id.files_activity_server_files_grid);
			    gridView.setAdapter(new FilesActivityFilesGridAdapter(fileList));				
			}
		});
	}
	
	@Override
	public void setLocalFilesCurrentDir(final String currentdir) {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				TextView textView = (TextView) viewPagerList.get(LOCAL_FILES_PAGE).findViewById(R.id.files_activity_local_current_directry);
				textView.setText(currentdir);
			}
		});
		
	}
	
	@Override
	public void setServerFilesCurrentDir(final String currentdir) {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				TextView textView = (TextView) viewPagerList.get(SERVER_FILES_PAGE).findViewById(R.id.files_activity_server_current_directry);
				textView.setText(currentdir);
			}
		});
	}
	
	@Override
	public void showLocalFilesGrid() {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				GridView gridView = (GridView) viewPagerList.get(LOCAL_FILES_PAGE).findViewById(R.id.files_activity_local_files_grid);
				TextView textView = (TextView) viewPagerList.get(LOCAL_FILES_PAGE).findViewById(R.id.files_activity_local_files_error_text);
				ProgressBar progressBar = (ProgressBar) viewPagerList.get(LOCAL_FILES_PAGE).findViewById(R.id.files_activity_local_filesprogress_bar);
				
				textView.setVisibility(View.GONE);
				progressBar.setVisibility(View.GONE);
				gridView.setVisibility(View.VISIBLE);
			}
		});		
	}
	
	@Override
	public void showLocalFilesGridProgressBar() {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				GridView gridView = (GridView) viewPagerList.get(LOCAL_FILES_PAGE).findViewById(R.id.files_activity_local_files_grid);
				TextView textView = (TextView) viewPagerList.get(LOCAL_FILES_PAGE).findViewById(R.id.files_activity_local_files_error_text);
				ProgressBar progressBar = (ProgressBar) viewPagerList.get(LOCAL_FILES_PAGE).findViewById(R.id.files_activity_local_filesprogress_bar);
				
				gridView.setVisibility(View.GONE);
				textView.setVisibility(View.GONE);
				progressBar.setVisibility(View.VISIBLE);
			}
		});	
	}
	
	@Override
	public void showLocalFilesGridErrorText() {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				GridView gridView = (GridView) viewPagerList.get(LOCAL_FILES_PAGE).findViewById(R.id.files_activity_local_files_grid);
				TextView textView = (TextView) viewPagerList.get(LOCAL_FILES_PAGE).findViewById(R.id.files_activity_local_files_error_text);
				ProgressBar progressBar = (ProgressBar) viewPagerList.get(LOCAL_FILES_PAGE).findViewById(R.id.files_activity_local_filesprogress_bar);
				
				gridView.setVisibility(View.GONE);
				progressBar.setVisibility(View.GONE);
				textView.setVisibility(View.VISIBLE);
			}
		});
	}
	
	@Override
	public void showServerFilesGrid() {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				GridView gridView = (GridView) viewPagerList.get(SERVER_FILES_PAGE).findViewById(R.id.files_activity_server_files_grid);
				TextView textView = (TextView) viewPagerList.get(SERVER_FILES_PAGE).findViewById(R.id.files_activity_server_files_error_text);
				ProgressBar progressBar = (ProgressBar) viewPagerList.get(SERVER_FILES_PAGE).findViewById(R.id.files_activity_server_filesprogress_bar);
				
				textView.setVisibility(View.GONE);
				progressBar.setVisibility(View.GONE);
				gridView.setVisibility(View.VISIBLE);
			}
		});		
	}
	
	@Override
	public void showServerFilesGridProgressBar() {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				GridView gridView = (GridView) viewPagerList.get(SERVER_FILES_PAGE).findViewById(R.id.files_activity_server_files_grid);
				TextView textView = (TextView) viewPagerList.get(SERVER_FILES_PAGE).findViewById(R.id.files_activity_server_files_error_text);
				ProgressBar progressBar = (ProgressBar) viewPagerList.get(SERVER_FILES_PAGE).findViewById(R.id.files_activity_server_filesprogress_bar);
				
				gridView.setVisibility(View.GONE);
				textView.setVisibility(View.GONE);
				progressBar.setVisibility(View.VISIBLE);
			}
		});	
	}
	
	@Override
	public void showServerFilesGridErrorText() {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				GridView gridView = (GridView) viewPagerList.get(SERVER_FILES_PAGE).findViewById(R.id.files_activity_server_files_grid);
				TextView textView = (TextView) viewPagerList.get(SERVER_FILES_PAGE).findViewById(R.id.files_activity_server_files_error_text);
				ProgressBar progressBar = (ProgressBar) viewPagerList.get(SERVER_FILES_PAGE).findViewById(R.id.files_activity_server_filesprogress_bar);
				
				gridView.setVisibility(View.GONE);
				progressBar.setVisibility(View.GONE);
				textView.setVisibility(View.VISIBLE);
			}
		});
	}
	
	@Override
	public void setLocalFileGridErrorText(final String message) {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				TextView textView = (TextView) viewPagerList.get(LOCAL_FILES_PAGE).findViewById(R.id.files_activity_local_files_error_text);
				textView.setText(message);
			}
		});
	}
	
	@Override
	public void setLocalFileGridErrorText(int messageID) {
		setLocalFileGridErrorText(getString(messageID));		
	}
	
	@Override
	public void setServerFileGridErrorText(final String message) {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				TextView textView = (TextView) viewPagerList.get(SERVER_FILES_PAGE).findViewById(R.id.files_activity_server_files_error_text);
				textView.setText(message);
			}
		});
	}
	
	@Override
	public void setServerFileGridErrorText(int messageID) {
		setServerFileGridErrorText(getString(messageID));
	}
	
	@Override
	public void setLocalSyncDirectorySetButtonListener(OnClickListener onClickListener) {
		viewPagerList.get(LOCAL_FILES_PAGE).findViewById(R.id.files_activity_local_sync_directry_set).setOnClickListener(onClickListener);
	}
}
